package com.clw.aspect;

import com.clw.exception.SellAuthorizeException;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017-12-18.
 */
@Aspect
@Component
public class AuthorizeAspect {

    @Pointcut("execution(public * com.clw.controller.*.*(..))"
            +
            "&& !execution(public * com.clw.controller.LoginController.*(..))")
    public void verify(){}

    @Before("verify()")
    public void doVerify(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session=request.getSession();
        Integer userId= (Integer) session.getAttribute("user_id");
        if (userId==null){
            throw new SellAuthorizeException();
        }
    }
}
